package com.winit.requirement.manager;

import com.winit.commons.util.SeqGenerate;
import com.winit.requirement.model.ReqRemark;
import com.winit.requirement.repository.ReqRemarkRepositoryExt;
import com.winit.requirement.repository.ReqRemarksRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class ReqRemarkManager {
    private static Logger log = LoggerFactory.getLogger(ReqRemarkManager.class);

    @Autowired
    private ReqRemarksRepository reqRemarksRepository;
    @Autowired
    private ReqRemarkRepositoryExt reqRemarkRepositoryExt;


    public ReqRemark createReqRemark(ReqRemark reqRemark){
        if (StringUtils.isEmpty(reqRemark.getId())){
            reqRemark.setId(SeqGenerate.createId("RMK"));
        }
        if (StringUtils.isEmpty(reqRemark.getNumber())){
            reqRemark.setNumber("RMK" + SeqGenerate.createSerialNo());
        }
        this.reqRemarksRepository.save(reqRemark);
        return reqRemark;
    }


    public ReqRemark updateReqRemark(ReqRemark reqRemark){
        this.reqRemarkRepositoryExt.updatePropertiesNotNullById(reqRemark);
        return reqRemark;
    }

    public List<ReqRemark> getReqRemarks(String id, String number, String reqId){
        return this.reqRemarksRepository.findByProperties(id, number, reqId);
    }


    public void deleteReqRemark(String id){
        if (null != this.reqRemarksRepository.findOne(id)) {
            this.reqRemarksRepository.delete(id);
        }
    }

}
